/*
 * $Id: CodeDetailsDAO.java,v 1.10 2009/07/10 14:50:16 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.database.dao;

import java.util.List;

import org.springmodules.cache.annotations.Cacheable;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.pojo.ActRequest;
import com.ge.healthcare.autosc.common.database.pojo.ActTask;
import com.ge.healthcare.autosc.common.database.pojo.CfgCodeDetails;
import com.ge.healthcare.autosc.common.database.pojo.CfgTaskType;
import com.ge.healthcare.autosc.common.exceptions.FaultRecorderException;

/**
 * @author 212042764
 * 
 */
public class CodeDetailsDAO extends BaseDAO implements ICodeDetailsDAO {


	/**
	 * Eclispe generated
	 */
	private static final long serialVersionUID = -2892879724479321998L;

	public CodeDetailsDAO() {
		super(CfgCodeDetails.TABLE_NAME);
	}
	@Cacheable(modelId="codeDetailsDAOCacheModel")
	public CfgCodeDetails getCodeDetailsByID(Long codeId) {
		if (codeId == null) {
			throw new FaultRecorderException(
					"Unable to lookup CodeDetails where Codeid is null value");
		}
		Object codeDetails = findObjectByID(CfgCodeDetails.COL_CODE_ID, codeId);
		if (!ASCUtil.isNullOrEmptyObject(codeDetails)) {
			return (CfgCodeDetails) codeDetails;
		}
		return null;
	}

	@Cacheable(modelId="codeDetailsDAOCacheModel")
	public CfgCodeDetails getUniqueCodedetailsByCodeName(String codeName) {
		if (ASCUtil.isNullOrEmpty(codeName)) {
			throw new FaultRecorderException(
					"Unable to lookup CodeDetails where Code Name is null or Empty");
		}
		List<?> codeDetailsList = findByProperty(CfgCodeDetails.COL_CODE_NAME, codeName);
		if (!ASCUtil.isNullOrEmptyList(codeDetailsList)) {
			CfgCodeDetails codeDetails = (CfgCodeDetails) codeDetailsList
					.get(0);
			return codeDetails;
		}
		return null;
	}
	@Cacheable(modelId="codeDetailsDAOCacheModel")
	public boolean isChildTaskType(ActTask task, CfgTaskType taskType) {
		if (CfgTaskType.ALWAYS_RUN_CODE.equals(taskType.getCodeGroup())) {
			return true;
		}
		CfgCodeDetails code = this.getCodeDetailsByID(task.getCodeId());
		if (code.getCodeGroup().equals(taskType.getCodeGroup())) {
			return true;
		}
		return false;
	}
	public boolean isComplete(ActTask task) {
		CfgCodeDetails code = this.getCodeDetailsByID(task.getCodeId());
		if (CfgCodeDetails.FAILURE_CODE_GROUP.equals(code.getCodeGroup())
				|| CfgCodeDetails.SUCCESS_CODE_GROUP
						.equals(code.getCodeGroup())) {
			return true;
		}
		return false;
	}
	
	public boolean isComplete(ActRequest request) {
		CfgCodeDetails code = this.getCodeDetailsByID(request.getCodeId());
		if (code.getCodeGroup().equals(CfgCodeDetails.FAILURE_CODE_GROUP)
				|| code.getCodeGroup()
						.equals(CfgCodeDetails.SUCCESS_CODE_GROUP)) {
			return true;
		}
		return false;
	}
	
	public boolean isRetry(ActTask task) {
		CfgCodeDetails code = this.getCodeDetailsByID(task.getCodeId());
		if (code.getCodeGroup().equals(CfgCodeDetails.RETRY_CODE_GROUP)) {
			return true;
		}
		return false;
	}

	public void save(Object pojo) {
		super.save(pojo);
	}

	public void saveAlways(Object pojo) {
		super.save(pojo);
	}

	public void deleteByID(Object pojo) {
		super.deleteByID(pojo);
	}

	public void refresh(Object pojo) {
		super.refresh(pojo);
	}
}
